package com.svms.vehicle;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.zkoss.util.resource.Labels;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Page;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.metainfo.ComponentInfo;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zul.Button;
import org.zkoss.zul.ListModel;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

import com.svms.service.vo.VehicleVO;
import com.svms.utils.SvmsMessageUtils;
import com.svms.web.constants.SvmsResources;
import com.svms.web.factory.SvmsWebFactory;

public class ManageVehicleCtrl extends GenericForwardComposer implements
		Serializable {

	private static final String VEHICLE_LIST_CTRL = "vehicleListCtrl";

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Window window_manage_vehicle;

	private Button btn_refresh;
	private Button btn_help;
	private Button btn_add_vehicle;
	private Button btn_search_vehicle_list;

	private Textbox txb_page_size;

	private Listbox lstbx_vehicles;
	private ListModel lst_model_vehicles;

	public Listbox getLstbx_vehicles() {
		return lstbx_vehicles;
	}

	public void setLstbx_vehicles(Listbox lstbx_vehicles) {
		this.lstbx_vehicles = lstbx_vehicles;
	}

	@Override
	public ComponentInfo doBeforeCompose(Page page, Component parent,
			ComponentInfo compInfo) {
		loadVehicletoListModel();
		return super.doBeforeCompose(page, parent, compInfo);
	}

	/**
	 * Populates the vehicle details list
	 * 
	 * @param event
	 * @throws Exception
	 */
	public void onCreate$window_manage_vehicle(Event event) throws Exception {
		setVehicleListModelToListBox();
		enableAllButtons();
	}

	/**
	 * To make a add vehicle call
	 * 
	 * @param event
	 * @throws Exception
	 */
	public void onClick$btn_add_vehicle(Event event) throws Exception {
		showDetailView(null);
	}

	/**
	 * To reset the vehicle list to current values in database
	 * 
	 * @param event
	 * @throws Exception
	 */
	public void onClick$btn_refresh(Event event) throws Exception {
		loadVehicletoListModel();
		setVehicleListModelToListBox();
	}

	/**
	 * To display help menu
	 * 
	 * @param event
	 * @throws Exception
	 */
	public void onClick$btn_help(Event event) throws Exception {
		StringBuffer helpMessage = new StringBuffer();
		helpMessage
				.append("---------------------------------------------------------");
		helpMessage.append("\n");
		helpMessage.append("1. Add Vehicle button to add a new vehicle");
		helpMessage.append("\n");
		helpMessage.append("2. Search to find avehicle in system");
		helpMessage.append("\n");
		helpMessage.append("3. Refresh to get a latest update from system");
		helpMessage.append("\n");
		helpMessage.append("4. Double click on the vehicle in list to edit it");
		helpMessage.append("\n");
		helpMessage
				.append("---------------------------------------------------------");
		SvmsMessageUtils.showGenericMessage(helpMessage.toString());
	}

	private void setVehicleListModelToListBox() {
		lstbx_vehicles.setModel(lst_model_vehicles);
		lstbx_vehicles.setItemRenderer(new VehicleListModelItemRender());
		lstbx_vehicles.setMold("paging");
		setPageSize();
	}

	private void setPageSize() {
		if (StringUtils.isNotBlank(txb_page_size.getValue())) {
			try {
				lstbx_vehicles.setPageSize(Integer.valueOf(txb_page_size
						.getValue()));
			} catch (Exception e) {
				SvmsMessageUtils
						.showErrorMessage("Page size can only be a number");
			}
		} else {
			lstbx_vehicles.setPageSize(15);
		}
	}

	private void loadVehicletoListModel() {
		List<VehicleVO> vehiclesFromService = SvmsWebFactory.vehicleService()
				.getVehicleList();
		lst_model_vehicles = new ListModelList<VehicleVO>(vehiclesFromService);
	}

	/**
	 * To set the paging size for the list
	 * 
	 * @param event
	 * @throws Exception
	 */
	public void onBlur$txb_page_size(Event event) throws Exception {
		setPageSize();
	}

	/**
	 * To search for vehicles from the main vehicle window
	 * 
	 * @param event
	 * @throws Exception
	 */
	public void onClick$btn_search_vehicle_list(Event event) throws Exception {
		Map<String, Object> args = new HashMap<String, Object>();
		args.put(VEHICLE_LIST_CTRL, this);
		// call the zul-file with the parameters packed in a map
		Window win = null;
		try {
			win = (Window) Executions.createComponents(
					SvmsResources.VEHICLE_SEARCH_DIALOGUE, null, args);
		} catch (final Exception e) {
			e.printStackTrace();
			SvmsMessageUtils.showErrorMessage(e.getMessage());
			if (win != null) {
				win.detach();
			}
		}

	}

	/**
	 * Forwarded from the {@link VehicleListModelItemRender} to perform the
	 * double click event on the list items
	 * 
	 * @param event
	 * @throws Exception
	 */
	public void onVehicleItemDoubleClicked(Event event) throws Exception {

		Listitem item = this.lstbx_vehicles.getSelectedItem();

		if (item != null) {
			VehicleVO vehicle = (VehicleVO) item.getAttribute("data");
			showDetailView(vehicle);
		} else {
			SvmsMessageUtils.show(Labels.getLabel("SVMS_1100"));
		}

	}

	/**
	 * Opens the detail view of the Vehicle Controller. <br>
	 * Overhanded some params in a map if needed. <br>
	 * 
	 * @param branche
	 * @throws Exception
	 */
	private void showDetailView(VehicleVO vehicle) throws Exception {

		final HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("selectedVehicle", vehicle);
		map.put(VEHICLE_LIST_CTRL, this);

		// call the zul-file with the parameters packed in a map
		Window win = null;
		try {
			win = (Window) Executions.createComponents(
					SvmsResources.MANAGE_VEHICLE_DIALOGUE, null, map);
		} catch (final Exception e) {
			e.printStackTrace();
			SvmsMessageUtils.showErrorMessage(e.getMessage());
			if (win != null) {
				win.detach();
			}
		}
	}

	private void enableAllButtons() {
		btn_refresh.setDisabled(false);
		btn_help.setDisabled(false);
		btn_add_vehicle.setDisabled(false);
		btn_search_vehicle_list.setDisabled(false);
		txb_page_size.setDisabled(false);
		txb_page_size.setValue(null);
		txb_page_size.setMaxlength(2);
	}

}
